<template>
  <div>
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon @click="$router.go(-1)" class="left-icon" slot="left" name="cross"></van-icon>
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="loginForm">

      <van-field v-model="user.mobile" name="mobile" label="用户名" placeholder="请输入手机号" :rules="userFormRules.mobile"
        type="number" maxlength="11">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
      </van-field>

      <van-field v-model="user.code" name="code" label="密码" placeholder="密码" :rules="userFormRules.code" type="number"
        maxlength="6">
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down format="ss s" @finish="isShowCountDown =false" :time="60 *1000" v-if="isShowCountDown" />
          <van-button v-else class="send-sms-btn" round size="small" type="default" native-type="button"
            @click="clickCode">
            发送验证码</van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '@/app/user.js'
export default {
  data () {
    return {
      // username: '',
      // password: '',
      isShowCountDown: false,
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      }
    }
  },
  methods: {
    // 表单验证
    async onSubmit (values) {
      // console.log('submit', values)
      // 获取表单数据
      // 验证
      // 请求
      // 成功处理
      // request({
      //   method: 'POST',
      //   url: '/app/v1_0/authorizations',
      //   data: {
      //     mobile: this.mobile,
      //     code: this.code
      //   }
      // })
      // 表单提交验证
      // 加载的时候提示加载数据

      this.$toast.loading({
        message: '加载中...',
        // 展示时长(ms)，值为 0 时，toast 不会消失
        duration: 0,
        forbidClick: true
      })
      // 登录
      try {
        const res = await login(this.user)
        // 发送请求psot
        // 把res数据和token存到vuex用initUser方法调vuex
        // 存完以后跳转到my页面
        console.log(res)
        this.$store.commit('initUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
      } catch (error) {
        console.log(error)
        if (error.response && error.response.status === 400) {
          return this.$toast.fail('亲账号和密码错误')
        }
        this.$toast.fail('登录失败')
        console.log(error)
      }
    },
    // 验证码
    async clickCode () {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return
      }
      this.$toast.loading({
        message: '发送中...',
        duration: 0,
        forbidClick: true
      })
      // 验证码的请求
      try {
        await sendCode(this.mobile)
        // 显示倒计时
        this.isShowCountDown = true
        this.$toast.success('成功发送')
      } catch (err) {
        if (err.response && err.response.status === 429) { return this.$toast.fail('发送频繁') }
      }
      this.$toast.fail('系统异常')
      // console.log('校验成功')
      // this.isShowCountDown = true
      // sendCode(this.mobile)
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
.send-sms-btn {
  width: 152px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
  .left-icon {
    color: #fff;
  }
}
.login-btn-wrap {
  padding: 53px 33px;
  .login-btn {
    background-color: #6db4fb;
    border: none;
  }
}
</style>
